<template>
	<view class="content">
		<view class="search-box">
			<view class="search-wrap">
				<u-input placeholder="请输入内容" border="surround" v-model="goodsName" @confirm="searchChange"
					shape='circle' inputAlign='center'>
					<template slot="suffix">
						<u-icon name="search" color="#7B77FE" size="40rpx" @click="searchChange">
						</u-icon>
					</template>
				</u-input>
			</view>
		</view>
		<view class="friend-goods-list" v-if="goodsList.length>0">
			<view v-for="(item, index) in goodsList" :key="index" class="goods-item" @click="navToDetailPage(item)">
				<view class="image-wrapper">
					<image :src="item.goodsImage" mode="aspectFill"></image>
				</view>
				<text class="title clamp">{{item.goodsName}}</text>
				<text class="sub-title clamp">{{item.goodsBrief}}</text>
				<view class="price-box">
					<text class="price">{{item.goodsPrice}}</text>
				</view>

			</view>
		</view>
		<u-empty v-if="goodsList.length==0" mode="data" text="未查询到数据,请下拉刷新" textSize="40rpx" iconSize="100rpx"
			marginTop="250">
		</u-empty>
		<uni-load-more :status="loadingType"></uni-load-more>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	export default {
		components: {},
		data() {
			return {
				loadingType: 'more', //加载更多状态
				cateList: [],
				goodsList: [],
				storeId: '', //门店id
				pageNo: 1,
				pageSize: 10,
				total: 0,
				goodsName: '',
				keyword: ''
			};
		},
		onLoad(opt) {
			console.log('onload -opt', opt);
			uni.showLoading({
				title: '加载中'
			})
			this.pageNo = 1
			this.getGoodList()
		},
		//加载更多
		onReachBottom() {
			if (this.pageNo * this.pageSize > this.total) {
				return uni.showToast({
					title: '没有更多数据',
					icon: 'none'
				})
			} else {
				this.pageNo++
				this.getGoodList()
			}

		},
		onPullDownRefresh() {
			this.getGoodListReset()
		},
		onShow() {
			console.log('onshow');
			uni.hideLoading()
		},
		computed: {
			...mapState(['otherUserInfo'])
		},
		methods: {
			//搜索
			searchChange() {
				let data = {
					openId: this.otherUserInfo.openId,
					status: '',
					goodsName: this.goodsName,
					pageNo: 1,
					pageSize: 20
				}
				this.$api.getYellowCarLists(data).then(
					res => {
						console.log(res)

						if (res.success) {
							console.log('我的店铺结果', res.result);
							// this.goodsList = res.result.records
							this.goodsList = res.result.records
							this.total = res.result.total
						}
					})
			},

			// 重置查询
			async getGoodListReset() {
				this.goodsName = ''
				this.pageNo = 1
				let data = {
					openId: this.otherUserInfo.openId,
					pageNo: this.pageNo,
					pageSize: this.pageSize
				}
				let res = await this.$api.getYellowCarLists(data)
				if (res.success) {
					console.log('重置查询我的店铺结果', res.result);
					this.goodsList = res.result.records
					this.total = res.result.total
				} else {
					this.goodsList = []
					this.total = 0
				}
				uni.stopPullDownRefresh()
			},
			// 根据storeId获取门店商品信息
			getGoodList() {
				let data = {
					openId: this.otherUserInfo.openId,
					pageNo: this.pageNo,
					pageSize: this.pageSize
				}
				this.$api.getYellowCarLists(data).then(
					res => {
						if (res.success) {
							console.log('我的店铺结果', res.result);
							this.goodsList = [...this.goodsList, ...res.result.records]
							this.total = res.result.total
						} else {
							this.goodsList = []
							this.total = 0
						}
					})
			},
			//加载商品 ，带下拉刷新和上滑加载
			async loadData(type = 'add', loading) {
				//没有更多直接返回
				if (type === 'add') {
					if (this.loadingType === 'nomore') {
						return;
					}
					this.loadingType = 'loading';
				} else {
					this.loadingType = 'more'
				}
				this.getGoodList()
				let goodsList = this.goodsList
				if (type === 'refresh') {
					this.goodsList = [];
				}

				this.goodsList = this.goodsList.concat(goodsList);

				//判断是否还有下一页，有是more  没有是nomore(测试数据判断大于20就没有了)
				this.loadingType = this.goodsList.length > this.pageSize ? 'nomore' : 'more';
				if (type === 'refresh') {
					if (loading == 1) {
						uni.hideLoading()
					} else {
						uni.stopPullDownRefresh();
					}
				}
			},
			//详情
			navToDetailPage(item) {
				console.log(item)
				this.toDishesInfo(item)
			},
			// 跳转优品会小程序商品详情页面
			async toDishesInfo(item) {
				console.log("这是微信跳转")
				console.log(item)
				let openId = uni.getStorageSync('openId')
				console.log("这是openId", openId)
				let flag = "1"
				wx.navigateToMiniProgram({
					appId: 'wxf5b2071cea9a5289',
					path: `standard/product/detail?productId=${item.defaultProductId}&goodsId=${item.goodsId}&openId=${openId.openid}&unionId=${openId.unionId}&flag=${flag}`,
					envVersion: __wxConfig.envVersion, //获取当前用户的小程序版本（开发，体验，线上版）
					success(res) {
						// 打开成功
						console.log('跳转成功', res)
					},
					fail: err => {
						console.log('跳转失败', err);
					},
				})

			}
		},
	}
</script>


<style lang="scss">
	.content {
		padding-bottom: 120upx;

		.search-box {
			z-index: 99;
			position: fixed;
			background: white;
			top: 0;
			width: 100%;
			display: flex;
			justify-content: space-around;
			padding-bottom: 30upx;

			.search-wrap {
				width: auto;
				// background: red;
				width: 90%;
			}

			.operation {
				display: flex;
				// background: red;
				width: 20%;
				align-items: center;
			}
		}
	}

	.friend-goods-list {
		margin-top: 100upx;
		display: flex;
		flex-wrap: wrap;
		margin-bottom: 80upx;
		padding: 0 30upx;
		background: #fff;

		.list-null-box {
			width: 400upx;
			height: 82vh;
			// background: red;
			margin: 20upx auto;
			text-align: center;
			font-size: 26upx;

			img {
				width: 300upx;
				height: 300upx;
			}
		}

		.goods-item {
			display: flex;
			flex-direction: column;
			width: 48%;
			padding-bottom: 40upx;

			&:nth-child(2n+1) {
				margin-right: 4%;
			}
		}

		.image-wrapper {
			width: 100%;
			height: 330upx;
			border-radius: 3px;
			overflow: hidden;

			image {
				width: 100%;
				height: 100%;
				opacity: 1;
			}
		}

		.title {
			margin-top: 10upx;
			font-size: 26upx;
			// height: 100upx;
			color: #000000;
			opacity: 0.9;
			line-height: 50upx;
			text-overflow: -o-ellipsis-lastline;
			overflow: hidden;
			align-content: center;
			text-overflow: ellipsis;
			display: -webkit-box;
			-webkit-line-clamp: 2;
			-webkit-box-orient: vertical;
		}

		.sub-title {
			font-size: 20upx;
			color: rgba(0, 0, 0, 0.6);
			line-height: 50upx;
			text-overflow: -o-ellipsis-lastline;
			overflow: hidden;
			align-content: center;
			text-overflow: ellipsis;
			display: -webkit-box;
			-webkit-line-clamp: 2;
			-webkit-box-orient: vertical;
		}

		.price-box {
			display: flex;
			align-items: center;
			justify-content: space-between;
			padding-right: 10upx;
			font-size: 24upx;
			color: #999999;
			margin-top: 10rpx;

			.add-to-car {
				background: #ff0000;
				display: flex;
				align-items: center;
				font-size: 26upx;
				color: white;
				border-radius: 8upx;
				padding: 5upx 10upx;
			}
		}

		.price {
			font-size: 32upx;
			color: #DC2626;
			line-height: 1;

			&:before {
				content: '￥';
				font-size: 26upx;
			}
		}

		.store-name {
			margin-top: 10upx;
			font-size: 26upx;
			color: #000000;
		}
	}
</style>